package com.javatest;

import java.util.*;

/**
 * @author LL
 * 判断101-200之间有多少个素数，并输出所有素数
 * @create 2020-12-09 16:24
 */
public class Subject02 {
    public static Map<Integer,List<Integer>> selectPrimeNum(Integer a, Integer b){
        List<Integer> list = new ArrayList<>();
        List<Integer> list1 = new ArrayList<>();
        Map<Integer,List<Integer>> map = new HashMap<>();
        int sum = 0;
        int sum1 = 0;
        int c = a>b?b:a;
        int d = a>b?a:b;
        for (int i = c; i <= d; i++) {
            if (isPrimeNum(i)){
                sum+=1;
                list.add(i);
            }else{
                sum1+=1;
                list1.add(i);
            }
        }
        map.put(sum,list);
        map.put(sum1,list1);
        return map;
    }

    public static boolean isPrimeNum(Integer i){
        boolean flag = true;
        for (int j = 2; j <= i/2 ; j++) {
            if (i%j == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static String returnErr(int a){
        if (a < 0) {
            return "输入数字类型错误";
        }
        return "";
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        try {
            System.out.println("输入第一个正整数：");
            int i = sc.nextInt();
            if (i < 0) {
                System.out.println(returnErr(i));
                return;
            }
            System.out.println("输入第二个正整数：");
            int i1 = sc.nextInt();
            if (i1 < 0) {
                System.out.println(returnErr(i));
                return;
            }
            long l = System.currentTimeMillis();
            Map<Integer, List<Integer>> integerListMap = selectPrimeNum(i, i1);

            for (Map.Entry<Integer, List<Integer>> integerListEntry : integerListMap.entrySet()) {
                Integer key = integerListEntry.getKey();
                List<Integer> value1 = integerListEntry.getValue();
                if(isPrimeNum(value1.get(0))){
                    System.out.println("素数个数："+key.toString()+"个");
                }else{
                    System.out.println("其他个数："+key.toString()+"个");
                }
                for (Integer value : value1) {
                    System.out.println(value.toString());
                }
            }
/*            Iterator<Map.Entry<Integer,List<Integer>>> ent = integerListMap.entrySet().iterator();
            while(ent.hasNext()){
                Map.Entry<Integer,List<Integer>> entry = ent.next();
                System.out.println("素数有："+entry.getKey()+"个");
                for (Integer value : entry.getValue()) {
                    System.out.println(value.toString());
                }
            }*/
            long l1 = System.currentTimeMillis();
            System.out.println("需要时间："+(l1 - l)+" ms");
        }catch (InputMismatchException e){
            System.out.println("输入数字类型错误"+e );
        }
    }
}
